#!/bin/bash 

# 检查参数
ROOT_DIR=$PWD
OUTPUT_DIR_Perf=$PWD/perf
OUTPUT_DIR_Test=$PWD/test
export skip_rc=77
export kselftest_default_timeout=120

# 删除已有输出结果
# rm -rf perf
# rm -rf test

# 创建输出目录（如果不存在）
mkdir -p "$OUTPUT_DIR_Perf"
mkdir -p "$OUTPUT_DIR_Test"
COUNT=1
count_file=$(mktemp)  # 创建临时文件来存储计数器

# Iterate over the directories
# Find all .c files in the specified directory (not including subdirectories)
for executable in tests/*; do
    # Check if the .c file exists to avoid errors when no .c files are found
    # Check if the corresponding executable exists and is a file
    if [ -f "$executable" ] && [ -x "$executable" ]; then
        executable="${executable#./}"
        echo "$executable"
        cmd="./$executable"
        file="$executable"

        if [ -f "$OUTPUT_DIR_Test/$file.txt" ]; then
            echo "the test $executable has completed"
        else
            "$cmd" > "$file.txt" 2>&1
            mv "$file.txt" "$OUTPUT_DIR_Test"
        fi  # 关闭 if
            
        # 调用 perf 脚本
        if [ -f "$OUTPUT_DIR_Perf/$file.txt" ]; then
            echo "the perf test has completed"
        else
            $ROOT_DIR/perf.sh "$cmd" "$OUTPUT_DIR_Perf" "$executable"
        fi  # 关闭 if
            
        COUNT=$((COUNT + 1))
        echo "$COUNT" > "$count_file"
        echo "$COUNT $executable"
        echo ""
    fi  # 关闭 if
done  # 关闭 for

COUNT=$(cat "$count_file")
rm "$count_file"
echo "Total number of tests completed: $COUNT"